package co.yixiang.controller.buyer.trace;

import co.yixiang.common.utils.ResultUtil;
import co.yixiang.common.vo.ResultMessage;
import co.yixiang.modules.trace.entity.domain.TraceBreed;
import co.yixiang.modules.trace.entity.dto.TraceBreedQueryCriteria;
import co.yixiang.modules.trace.service.TraceBreedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "溯源:溯源管理")
@RestController
@RequestMapping("/trace")
public class TraceBuyerController {
    @Autowired
    private TraceBreedService traceBreedService;
    @ApiOperation(value = "查询养殖/耳标统计数据")
    @GetMapping(value = "/ranking")
    public ResultMessage<Map<String,Object>> traceRanking(){
        Map<String,Object> map = new HashMap<>();
        // 累计养殖数量
        final Long count = traceBreedService.lambdaQuery().count();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        final Long todayCount = traceBreedService.lambdaQuery().ge(TraceBreed::getCreateTime, calendar.getTime()).count();
        map.put("count", count);
        map.put("todayCount", todayCount);
        return ResultUtil.data(map);
    }

    @ApiOperation(value = "分页查询耳标数据")
    @GetMapping
    public ResultMessage<Map<String,Object>> tracePage(TraceBreedQueryCriteria criteria, Pageable pageable){
        final Map<String, Object> stringObjectMap = traceBreedService.queryAllByVo(criteria, pageable);
        return ResultUtil.data(stringObjectMap);
    }
}
